'''
Description: 
Version: 1.0.1
Autor: hrlu.cn
Date: 2022-07-30 14:11:12
LastEditors: hrlu.cn
LastEditTime: 2022-07-30 14:13:05
'''
from processor.scheduler import Scheduler
from processor.crawler import *
from processor.functions import *
from processor.models import Logger

from config import DEFAULT_START_DATE, USING_PUBLISHER

'''
    任务发布器
        任务：
            1. 刷新股票列表
                - 每日执行
                - 无权限控制
            2. 更新 Tushare 源
                - 受 config 中配置(USING_TUSHARE)控制
                - 服务启动时执行
                - 每个开盘日（去除节假日、周末）的 15:00 执行
            3. 更新 Wind 源
                - 受 config 中配置(USING_WIND)控制
                - 服务启动时执行
                - 每个开盘日（去除节假日、周末）的 15:00 执行
            4. MA 交叉筛选
                - 于每日数据源更新后执行
'''


if USING_PUBLISHER:
    ''' 启用 USING_PUBLISHER 时执行 '''
    today = datetime.date.today()
    if datetime.datetime.now().hour > 15:
        today += datetime.timedelta(days = 1)
    next_launch = datetime.datetime(today.year, today.month, today.day, 15)

    ''' 系统第一次执行时需要更新全部股票列表
    '''
    Logger.debug(msg="Publisher process started.", module='scheduler')
    
    Scheduler.set_timer(desp="刷新股票列表", func=TushareAPI.update_stock_list, args=("ALL", Scheduler.update_progress))

    # Scheduler.set_timer(desp = "更新 RSI", delay=0, func = WindAPI.update_all_stock_rsi, args = ("2020-01-01", "2021-01-01", Scheduler.update_progress, ))

    ''' 系统启动后若时间早于 12:00 则立即执行一次数据源更新
    '''
    '''
    if datetime.datetime.now().hour < 12:
        if USING_TUSHARE:
            Scheduler.set_timer(desp = "更新 Tushare 源", func = TushareAPI.update_all_stock_basic, args = (None, None, Scheduler.update_progress, ))
            Scheduler.set_timer(desp = "执行 Tushare 交叉筛选", func = MACross.sch_pcs, args = (today - datetime.timedelta(days = 1), today, 1, Scheduler.update_progress, ))
        if USING_WIND:
            Scheduler.set_timer(desp = "更新 Wind 源", func = WindAPI.update_all_stock_basic, args = (None, None, Scheduler.update_progress, ))
            Scheduler.set_timer(desp = "执行 Wind 交叉筛选", func = MACross.sch_pcs, args = (today - datetime.timedelta(days = 1), today, 2, Scheduler.update_progress, ))
    '''

    ''' 每日 15:00 执行一次数据源更新
    '''
    if USING_TUSHARE:
        Scheduler.set_timer(desp="更新 Tushare 源", delay=next_launch, itv=1, func=TushareAPI.update_all_stock_basic, args=(None, None, Scheduler.update_progress, ))
        Scheduler.set_timer(desp="执行 Tushare 交叉筛选", delay=next_launch, itv=1, func=MACross.sch_pcs, args=(today - datetime.timedelta(days=1), today, 1, Scheduler.update_progress, ))


    # Scheduler.set_timer(desp = "刷新股票列表", func = TushareAPI.update_stock_list, args = ("ALL", Scheduler.update_progress))
    # Scheduler.set_timer(desp = "更新股票数据", func = TushareAPI.update_all_stock_basic, args = (Scheduler.update_progress, ))

    # Scheduler.set_timer(desp = "更新股票数据", delay=18000, func = TushareAPI.update_all_stock_basic, args = (Scheduler.update_progress, ))
    # Scheduler.set_timer(desp = "更新股票数据", delay=19000, func = TushareAPI.update_all_stock_basic, args = (Scheduler.update_progress, ))
    # Scheduler.set_timer(desp = "更新股票数据", delay=20000, func = TushareAPI.update_all_stock_basic, args = (Scheduler.update_progress, ))